<?php
/*
+-----------------------------------------------------------------------------+
| $Id: *.php 2009-08-18 08:41:22Z Bleakwind $
| Copyright (c) 2003-2010 Bleakwind (www.weaverdream.com)
| http://www.weaverdream.com/
+-----------------------------------------------------------------------------+
*/

if (!defined( 'ENTRY_INDEX')){
    echo "<h1>Forbidden</h1><p>You don't have permission to access on this server.</p>";
    exit;
}

function cp_detail($value)
{
    global $SETTING,$LANGUAGE,$CONFIG,$CONFIGURE,$SESSION,$PROMPT,$MEMBER,$db,$sys,$c;
    $ajax_response = new xajaxResponse();
    $error = false;
    //func::fp($value);

    if (empty($value['realname'])) {
        $ajax_response->assign("realname_return", "innerHTML", "");
    } elseif ( mb_strwidth($value['realname'],"UTF-8") > 60 ||
        preg_match("/[\x01-\x2E]|[\\x2F]|[\x3A-\x40]|[\x5B-\x5E]|[\x60]|[\x7B-\x7F]/",$value['realname'])) {
        $error = true;
        if(empty($anchor)) { $anchor = "realname"; }
        $ajax_response->assign("realname_return", "innerHTML", "<span class=\"prompt_failed\">".$LANGUAGE['s']['cp_detail']['submit_realname_error']."</span>");
    } else {
        $ajax_response->assign("realname_return", "innerHTML", "<span class=\"prompt_succeed\">".$LANGUAGE['s']['cp_detail']['submit_realname_right']."</span>");
    }

    if( !preg_match("/^[123]{1}$/i",$value['gender']) ){
        $error = true;
        if(empty($anchor)) { $anchor = "gender"; }
        $ajax_response->assign("gender_return", "innerHTML", "<span class=\"prompt_failed\">".$LANGUAGE['s']['cp_detail']['submit_gender_error']."</span>");
    } else {
        $ajax_response->assign("gender_return", "innerHTML", "<span class=\"prompt_succeed\">".$LANGUAGE['s']['cp_detail']['submit_gender_right']."</span>");
    }

    if (empty($value['birthday'])) {
        $ajax_response->assign("birthday_return", "innerHTML", "");
    } elseif( !preg_match("/^[12][0-9][0-9][0-9]-[01][0-9]-[0-3][0-9]$/",$value['birthday']) ){
        $error = true;
        if(empty($anchor)) { $anchor = "birthday"; }
        $ajax_response->assign("birthday_return", "innerHTML", "<span class=\"prompt_failed\">".$LANGUAGE['s']['cp_detail']['submit_birthday_error']."</span>");
    } else {
        $ajax_response->assign("birthday_return", "innerHTML", "<span class=\"prompt_succeed\">".$LANGUAGE['s']['cp_detail']['submit_birthday_right']."</span>");
    }

    if (empty($value['address'])) {
        $ajax_response->assign("address_return", "innerHTML", "");
    } elseif ( mb_strwidth($value['address'],"UTF-8") > 255 ||
        preg_match("/[\x01-\x2E]|[\\x2F]|[\x3A-\x40]|[\x5B-\x5E]|[\x60]|[\x7B-\x7F]/",$value['address'])) {
        $error = true;
        if(empty($anchor)) { $anchor = "address"; }
        $ajax_response->assign("address_return", "innerHTML", "<span class=\"prompt_failed\">".$LANGUAGE['s']['cp_detail']['submit_address_error']."</span>");
    } else {
        $ajax_response->assign("address_return", "innerHTML", "<span class=\"prompt_succeed\">".$LANGUAGE['s']['cp_detail']['submit_address_right']."</span>");
    }

    if (empty($value['postalcode'])) {
        $ajax_response->assign("postalcode_return", "innerHTML", "");
    } elseif ( mb_strwidth($value['postalcode'],"UTF-8") > 60 ||
        preg_match("/[\x01-\x2E]|[\\x2F]|[\x3A-\x40]|[\x5B-\x5E]|[\x60]|[\x7B-\x7F]/",$value['postalcode'])) {
        $error = true;
        if(empty($anchor)) { $anchor = "postalcode"; }
        $ajax_response->assign("postalcode_return", "innerHTML", "<span class=\"prompt_failed\">".$LANGUAGE['s']['cp_detail']['submit_postalcode_error']."</span>");
    } else {
        $ajax_response->assign("postalcode_return", "innerHTML", "<span class=\"prompt_succeed\">".$LANGUAGE['s']['cp_detail']['submit_postalcode_right']."</span>");
    }

    if (empty($value['telephone'])) {
        $ajax_response->assign("telephone_return", "innerHTML", "");
    } elseif ( mb_strwidth($value['telephone'],"UTF-8") > 60 ||
        preg_match("/[\x01-\x2C]|[\x2E]|[\\x2F]|[\x3A-\x40]|[\x5B-\x60]|[\x7B-\x7F]/",$value['telephone'])) {
        $error = true;
        if(empty($anchor)) { $anchor = "telephone"; }
        $ajax_response->assign("telephone_return", "innerHTML", "<span class=\"prompt_failed\">".$LANGUAGE['s']['cp_detail']['submit_telephone_error']."</span>");
    } else {
        $ajax_response->assign("telephone_return", "innerHTML", "<span class=\"prompt_succeed\">".$LANGUAGE['s']['cp_detail']['submit_telephone_right']."</span>");
    }

    if (empty($value['mobile'])) {
        $ajax_response->assign("mobile_return", "innerHTML", "");
    } elseif ( mb_strwidth($value['mobile'],"UTF-8") > 60 ||
        preg_match("/[\x01-\x2C]|[\x2E]|[\\x2F]|[\x3A-\x40]|[\x5B-\x60]|[\x7B-\x7F]/",$value['mobile'])) {
        $error = true;
        if(empty($anchor)) { $anchor = "mobile"; }
        $ajax_response->assign("mobile_return", "innerHTML", "<span class=\"prompt_failed\">".$LANGUAGE['s']['cp_detail']['submit_mobile_error']."</span>");
    } else {
        $ajax_response->assign("mobile_return", "innerHTML", "<span class=\"prompt_succeed\">".$LANGUAGE['s']['cp_detail']['submit_mobile_error']."</span>");
    }

    if (empty($value['website'])) {
        $ajax_response->assign("website_return", "innerHTML", "");
    } elseif ( !preg_match("/^[a-z0-9-\.http:\/\/]+$/i",$value['website']) ) {
        $error = true;
        if(empty($anchor)) { $anchor = "website"; }
        $ajax_response->assign("website_return", "innerHTML", "<span class=\"prompt_failed\">".$LANGUAGE['s']['cp_detail']['submit_website_error']."</span>");
    } else {
        $ajax_response->assign("website_return", "innerHTML", "<span class=\"prompt_succeed\">".$LANGUAGE['s']['cp_detail']['submit_website_right']."</span>");
    }

    if (empty($value['gtalk'])) {
        $ajax_response->assign("gtalk_return", "innerHTML", "");
    } elseif ( !preg_match("/^[a-z0-9-_\.]+@([a-z0-9][a-z0-9-]*\.)+[a-z]{2,4}$/i",$value['gtalk']) ) {
        $error = true;
        if(empty($anchor)) { $anchor = "gtalk"; }
        $ajax_response->assign("gtalk_return", "innerHTML", "<span class=\"prompt_failed\">".$LANGUAGE['s']['cp_detail']['submit_gtalk_error']."</span>");
    } else {
        $ajax_response->assign("gtalk_return", "innerHTML", "<span class=\"prompt_succeed\">".$LANGUAGE['s']['cp_detail']['submit_gtalk_right']."</span>");
    }

    if (empty($value['msn'])) {
        $ajax_response->assign("msn_return", "innerHTML", "");
    } elseif ( !preg_match("/^[a-z0-9-_\.]+@([a-z0-9][a-z0-9-]*\.)+[a-z]{2,4}$/i",$value['msn']) ) {
        $error = true;
        if(empty($anchor)) { $anchor = "msn"; }
        $ajax_response->assign("msn_return", "innerHTML", "<span class=\"prompt_failed\">".$LANGUAGE['s']['cp_detail']['submit_msn_error']."</span>");
    } else {
        $ajax_response->assign("msn_return", "innerHTML", "<span class=\"prompt_succeed\">".$LANGUAGE['s']['cp_detail']['submit_msn_right']."</span>");
    }

    if (empty($value['icq'])) {
        $ajax_response->assign("icq_return", "innerHTML", "");
    } elseif( !preg_match("/^[0-9]{0,30}$/",$value['icq']) ){
        $error = true;
        if(empty($anchor)) { $anchor = "icq"; }
        $ajax_response->assign("icq_return", "innerHTML", "<span class=\"prompt_failed\">".$LANGUAGE['s']['cp_detail']['submit_icq_error']."</span>");
    } else {
        $ajax_response->assign("icq_return", "innerHTML", "<span class=\"prompt_succeed\">".$LANGUAGE['s']['cp_detail']['submit_icq_right']."</span>");
    }

    if (empty($value['qq'])) {
        $ajax_response->assign("qq_return", "innerHTML", "");
    } elseif( !preg_match("/^[0-9]{0,30}$/",$value['qq']) ){
        $error = true;
        if(empty($anchor)) { $anchor = "qq"; }
        $ajax_response->assign("qq_return", "innerHTML", "<span class=\"prompt_failed\">".$LANGUAGE['s']['cp_detail']['submit_qq_error']."</span>");
    } else {
        $ajax_response->assign("qq_return", "innerHTML", "<span class=\"prompt_succeed\">".$LANGUAGE['s']['cp_detail']['submit_qq_right']."</span>");
    }

    if (empty($value['yahoo'])) {
        $ajax_response->assign("yahoo_return", "innerHTML", "");
    } elseif ( !preg_match("/^[a-z0-9-_\.]+@([a-z0-9][a-z0-9-]*\.)+[a-z]{2,4}$/i",$value['yahoo']) ) {
        $error = true;
        if(empty($anchor)) { $anchor = "yahoo"; }
        $ajax_response->assign("yahoo_return", "innerHTML", "<span class=\"prompt_failed\">".$LANGUAGE['s']['cp_detail']['submit_yahoo_error']."</span>");
    } else {
        $ajax_response->assign("yahoo_return", "innerHTML", "<span class=\"prompt_succeed\">".$LANGUAGE['s']['cp_detail']['submit_yahoo_right']."</span>");
    }

    if (mb_strlen($value['bio'], "utf-8") > $CONFIG['max_member_bio']) {
        $error = true;
        if(empty($anchor)) { $anchor = "bio"; }
        $ajax_response->assign("bio_return", "innerHTML", "<span class=\"prompt_failed\">".sprintf($LANGUAGE['s']['cp_detail']['submit_bio_error'],$CONFIG['max_member_bio'])."</span>");
    } else {
        $ajax_response->assign("bio_return", "innerHTML", "<span class=\"prompt_succeed\">".$LANGUAGE['s']['cp_detail']['submit_bio_right']."</span>");
    }

    if (mb_strlen($value['sign'], "utf-8") > $CONFIG['max_member_sign']) {
        $error = true;
        if(empty($anchor)) { $anchor = "sign"; }
        $ajax_response->assign("sign_return", "innerHTML", "<span class=\"prompt_failed\">".sprintf($LANGUAGE['s']['cp_detail']['submit_sign_error'],$CONFIG['max_member_sign'])."</span>");
    } else {
        $ajax_response->assign("sign_return", "innerHTML", "<span class=\"prompt_succeed\">".$LANGUAGE['s']['cp_detail']['submit_sign_right']."</span>");
    }

    if($error){
        $ajax_response->assign("cp_detail_submit","value",$LANGUAGE['s']['cp_detail']['submit_amend']);
        $ajax_response->assign("cp_detail_submit","disabled",false);
        $ajax_response->redirect("#".$anchor);
    }else{

        $config_list = func::db_select(DB_TABLE_CONFIG, "name,value", "name='censor_post'");
        if (!empty($config_list)) {
            foreach($config_list as $v){
                $CONFIG[$v['name']] = trim($v['value']);
            }
        }
        $str_search = preg_match_all('/(.*)=/i', $CONFIG['censor_post'], $matches);
        $str_search = $matches[1];
        $str_replace = preg_match_all('/=(.*)/i', $CONFIG['censor_post'], $matches);
        $str_replace = $matches[1];
        for($i=0; $size=count($str_search),$i<$size;$i++) {
            $value['realname']  = eregi_replace($str_search[$i], $str_replace[$i], $value['realname']);
            $value['bio']       = eregi_replace($str_search[$i], $str_replace[$i], $value['bio']);
            $value['sign']      = eregi_replace($str_search[$i], $str_replace[$i], $value['sign']);
        }

        //////
        $sql = "UPDATE ".DB_TABLE_MEMBER_DETAIL." SET
                    realname        ='".addslashes(func::str_stripbr($value['realname']))."',
                    gender          ='".addslashes($value['gender'])."',
                    birthday        ='".strtotime($value['birthday'])."',
                    address         ='".addslashes(func::str_stripbr($value['address']))."',
                    postalcode      ='".addslashes(func::str_stripbr($value['postalcode']))."',
                    telephone       ='".addslashes(func::str_stripbr($value['telephone']))."',
                    mobile          ='".addslashes(func::str_stripbr($value['mobile']))."',
                    website         ='".addslashes($value['website'])."',
                    gtalk           ='".addslashes($value['gtalk'])."',
                    msn             ='".addslashes($value['msn'])."',
                    icq             ='".addslashes($value['icq'])."',
                    qq              ='".addslashes($value['qq'])."',
                    yahoo           ='".addslashes($value['yahoo'])."',
                    bio             ='".addslashes($value['bio'])."',
                    sign            ='".addslashes($value['sign'])."'
                WHERE mid='".$MEMBER['id']."'";
        $result = &$db->Execute($sql);
        if (!$result) {
            $ajax_response->alert($db->ErrorMsg());
        }else{
            $sys->set_prompt(array(
                "prompt_type"       => "'cp_prompt'",
                "prompt_if"         => "1",
                "prompt_message"    => "'".$LANGUAGE['s']['cp_detail']['submit_amend_succeed']."'",
                "prompt_returnurl"  => "'".$CONFIGURE['common']['control_index']."?act=cp'",
                "prompt_returntext" => "'".$LANGUAGE['s']['cp_detail']['submit_amend_return']."'",
                "prompt_image"      => "'succeed.gif'",
                "prompt_act"        => "'cp_prompt'",
            ));
            $ajax_response->redirect($CONFIGURE['common']['control_index']."?act=cp_prompt");
        }
        //////
    }

    return $ajax_response;
}
$bwajax->register(XAJAX_FUNCTION, "cp_detail");
$public_var['page_place'][] = $LANGUAGE['s']['cp_detail']['page_place_upper'];
$public_var['page_place'][] = $LANGUAGE['s']['cp_detail']['page_place'];
?>
